package com.scott.customer;

import com.scott.customer.service.MyHystrix;
import com.scott.customer.service.MyHystrixDetail;
import com.scott.customer.service.SayBayService;
import com.scott.customer.service.SayHelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

@RestController("/")
@EnableEurekaClient
@EnableFeignClients
@SpringBootApplication
public class CustomerApplication {

    @Autowired
    SayHelloService sayHelloService;
    @Autowired
    SayBayService sayBayService;
    @Autowired
    RestTemplate restTemplate;

    public static void main(String[] args) {
        SpringApplication.run(CustomerApplication.class, args);
    }

    @GetMapping("/hello")
    public String hello(){
        return "hello word customer";
    }

    @GetMapping("/sayProvider")
    public String sayProvider(){
        String hello = sayHelloService.findHello();

        String bay = sayBayService.findBay();
        return hello+"__||__"+bay;
    }

    @GetMapping("/my/say")
    public String sayMy(){
        System.out.println("test");
        MyHystrix myHystrix = new MyHystrix("say", restTemplate);
        return myHystrix.execute();
    }
    @GetMapping("/my/say2")
    public String sayMy2() throws ExecutionException, InterruptedException {
        System.out.println("test");
        MyHystrix myHystrix = new MyHystrix("say", restTemplate);
        Future<String> result =myHystrix.queue();
        return result.get();
    }

    @GetMapping("/my/detail")
    public String sayDetail(){
        System.out.println("detail");
        MyHystrixDetail myHystrixDetail = new MyHystrixDetail(sayHelloService);
        return myHystrixDetail.execute();
    }


}
